package com.foxzzz.operation;

import com.foxzzz.database.SqlExecutor;

import java.util.List;
import java.util.Map;

public class PlayerDataBaseOperation {
    public final static String INSERT_INTO_ITEM_STORE_DATA="INSERT INTO ITEM_STORE_DATA VALUES(?,?,?,?,?)";
    public final static String INSERT_INTO_ITEM_SHOP_DATA="INSERT INTO ITEM_SHOP_DATA VALUES(?,?,?,?,?,?)";
    public final static String INSERT_INTO_ACCOUNT="INSERT INTO ACCOUNT VALUES(?,?)";
    public final static String UPDATE_ITEM_STORE_DATA="UPDATE ITEM_STORE_DATA SET ID=?,NAME=?,PLAYER=?,NBT=?,NUM=NUM+? WHERE ID=? AND NBT=?";
    public final static String UPDATE_ITEM_SHOP_DATA_SELL="UPDATE ITEM_SHOP_DATA SET ID=?,NAME=?,NBT=?,PLAYER=?,PRICE=PRICE+?,STORE_NUM=STORE_NUM+? WHERE ID=? AND NBT=?";
    public final static String UPDATE_ITEM_SHOP_DATA_BUY="UPDATE ITEM_SHOP_DATA SET STORE_NUM=STORE_NUM-? WHERE ID=?";

    public final static String UPDATE_ACCOUNT="UPDATE ACCOUNT SET VALUE=VALUE+? WHERE PLAYER=?";
    public final static String DELETE_ITEM_STORE_DATA="DELETE ITEM_STORE_DATA WHERE ID=?";
    public final static String DELETE_ITEM_SHOP_DATA="DELETE ITEM_SHOP_DATA WHERE ID=?";
    public final static String QUERY_VALUE_FROM_ACCOUNT="SELECT VALUE FROM ACCOUNT WHERE PLAYER=?";
    public final static String QUERY_COUNT_FROM_ACCOUNT="SELECT COUNT(*) FROM ACCOUNT";
    public final static String QUERY_FROM_ACCOUNT="SELECT * FROM ACCOUNT ORDER BY VALUE DESC LIMIT ?,?";

    public final static String QUERY_ITEM_STORE_DATA_BY_NBT="SELECT NUM,NAME FROM ITEM_STORE_DATA WHERE NBT=? AND PLAYER=?";
    public final static String QUERY_ITEM_STORE_DATA_BY_NAME="SELECT * FROM ITEM_STORE_DATA WHERE NAME=? AND PLAYER=?";
    public final static String QUERY_COUNT_FROM_ITEM_STORE_DATA_BY_PLAYER="SELECT COUNT(*) FROM ITEM_STORE_DATA WHERE PLAYER=?";
    public final static String QUERY_ITEM_STORE_DATA_BY_PLAYER="SELECT NAME,NUM FROM ITEM_STORE_DATA WHERE PLAYER=? ORDER BY NUM DESC LIMIT ?,?";

    public final static String QUERY_COUNT_FROM_ITEM_SHOP_DATA_BY_PLAYER="SELECT COUNT(*) FROM ITEM_STORE_DATA WHERE PLAYER=?";
    public final static String QUERY_ITEM_SHOP_BY_PLAYER="SELECT * FROM ITEM_SHOP_DATA WHERE PLAYER=? ";
    public final static String QUERY_ITEM_SHOP_BY_ID_NBT="SELECT * FROM ITEM_SHOP_DATA WHERE PLAYER=?,NBT=?";
    public final static String QUERY_ITEM_SHOP_BY_PRICE="SELECT * FROM ITEM_SHOP_DATA WHERE NBT=? ORDER BY PRICE";

    private String player=null;
    public PlayerDataBaseOperation(String player){
        List list=SqlExecutor.queryBySql(QUERY_VALUE_FROM_ACCOUNT,player);
        //用户账户不存在 初始化用户账户
        if(list.size()==0)
        SqlExecutor.updateBySql(INSERT_INTO_ACCOUNT,player,0);
        this.player=player;
    }
    //--银行部分功能
    //检查玩家账号是否存在
    public boolean exsitPlayerAccount(String player){
        List list=SqlExecutor.queryBySql(QUERY_VALUE_FROM_ACCOUNT,player);
        return list.size()!=0;
    }

    //变值
    public String addAccount(long num){
        SqlExecutor.updateBySql(UPDATE_ACCOUNT,num,player);
        return Msg.SUCCESS;
    }
    //变值1
    public String addAccountByPlayer(String player,long num){
        SqlExecutor.updateBySql(UPDATE_ACCOUNT,num,player);
        return Msg.SUCCESS;
    }


    //查询余额
    public Long getAccount(){
       List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_VALUE_FROM_ACCOUNT,player);
       if(list.size()==0)
           return 0L;
       else
           return (Long) list.get(0).get("VALUE");
    }
    //查询总页数
    public int getAccountTotal(){
        String str=SqlExecutor.queryBySql(QUERY_COUNT_FROM_ACCOUNT).get(0).get("COUNT(*)").toString();
        int num=Integer.parseInt(str);
        int extra=num%10==0?0:1;
        return num/10+extra;
    }
    //分页查询排行
    public List<Map<String,Object>> getAccountRank(int page){
        page=page<1?1:page;
        int end=page*10;
        int start=end-10;
        start=start<0?0:start;
        return SqlExecutor.queryBySql(QUERY_FROM_ACCOUNT,start,10);
    }


    //查询一个人的存储总数
    public int getStoreNumByPlayer(String player){
        String str=SqlExecutor.queryBySql(QUERY_COUNT_FROM_ITEM_STORE_DATA_BY_PLAYER,player).get(0).get("COUNT(*)").toString();
        int num=Integer.parseInt(str);
        int extra=num%10==0?0:1;
        return num/10+extra;
    }
    //查询所有人的存储总数
    public List<Map<String,Object>> getAllStoreByPlayer(String player,int page){
        page=page<1?1:page;
        int end=page*10;
        int start=end-10;
        start=start<0?0:start;
        return SqlExecutor.queryBySql(QUERY_ITEM_STORE_DATA_BY_PLAYER,player,start,10);
    }



    //根据名字获取nbt和num
    public Map<String,Object> getNbtAndNumByName(String name){
        List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_ITEM_STORE_DATA_BY_NAME,name,player);
        return list.size()==0?null:list.get(0);
    }
    //根据PLAYER和NBT来获取对方此条记录
    public Map<String,Object> getStoreByPlayerAndNbt(String player,String nbt){
        System.out.println(nbt);
        List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_ITEM_STORE_DATA_BY_NBT,nbt,player);
        return list.size()==0?null:list.get(0);
    }


    //更新数据 改名,变值  count限制条数
    public String upsertStore(String name,long num,String nbt,long count){
        return upsertStore(player,name,num,nbt,count);
    }

    //
    //更新数据 改名,变值  count限制条数
    public String upsertStore(String player,String name,long num,String nbt,long count){
        List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_ITEM_STORE_DATA_BY_NBT,nbt,player);
        if(list.size()==0){
            List<Map<String,Object>> list1=SqlExecutor.queryBySql(QUERY_COUNT_FROM_ITEM_STORE_DATA_BY_PLAYER,player);
            int row= Integer.parseInt(list1.get(0).get("COUNT(*)").toString());
            if(row<count)
                SqlExecutor.updateBySql(INSERT_INTO_ITEM_STORE_DATA,player+"-"+name,name,player,nbt,num);
            else return Msg.LIMIT;
        }else{
            int num1= Integer.parseInt(list.get(0).get("NUM").toString());
            if(num1+num<=0)
                SqlExecutor.updateBySql(DELETE_ITEM_STORE_DATA,player+"-"+name);
            else
                SqlExecutor.updateBySql(UPDATE_ITEM_STORE_DATA,player+"-"+name,name,player,nbt,num,player+"-"+name,nbt);
        }
        return Msg.SUCCESS;
    }


    //删除记录
    public String deleteStore(String name){
        SqlExecutor.updateBySql(DELETE_ITEM_STORE_DATA,player+"-"+name);
        return Msg.SUCCESS;
    }

    //根据玩家查询商店
    public List<Map<String,Object>> queryShopByPlayer(String player){
        return SqlExecutor.queryBySql(QUERY_ITEM_SHOP_BY_PLAYER,player);
    }

    //根据物品查询商店
    public List<Map<String,Object>> queryShopByItem(String nbt){
        return SqlExecutor.queryBySql(QUERY_ITEM_SHOP_BY_PRICE,nbt);
    }

    //更新库存(购买行为)
    public String buyUpdate(int num,String id){
        SqlExecutor.updateBySql(UPDATE_ITEM_SHOP_DATA_BUY,num,id);
        return Msg.SUCCESS;
    }

    //更新库存 (自己修改商店)
    public String upsertShop(String name,String nbt,long price,long storeNum,long count){
        List list=SqlExecutor.queryBySql(QUERY_ITEM_STORE_DATA_BY_NBT,player,nbt);
        if(list.size()==0){
            List<Map<String,Object>> list1=SqlExecutor.queryBySql(QUERY_COUNT_FROM_ITEM_SHOP_DATA_BY_PLAYER,player);
            Integer row= Integer.parseInt(list1.get(0).get("COUNT(*)").toString()) ;
            if(row<count)
                SqlExecutor.updateBySql(INSERT_INTO_ITEM_SHOP_DATA,player+"-"+name,name,nbt,player,price,storeNum);
            else return Msg.LIMIT;
        }else{
           SqlExecutor.updateBySql(UPDATE_ITEM_SHOP_DATA_SELL,player+"-"+name,name,nbt,player,price,storeNum,player+"-"+name,nbt);
        }
        return Msg.SUCCESS;

    }

    //下架商品
    public String delete(String name){
        SqlExecutor.updateBySql(DELETE_ITEM_SHOP_DATA,player+"-"+name);
        return Msg.SUCCESS;
    }
}
